#!/usr/bin/perl
# Version 0.2.1

$testcase = "bad_ls_req";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 2) {
        print("Usage: $testcase.pl dump_file1 dump_file2 log_file");
        exit 1;
}

$dumpfile1 = $ARGV[0];
$dumpfile2 = $ARGV[1];
$logfile = $ARGV[2];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3 );

$success = 1; 
$state = 0;

open(DUMPFILE, $dumpfile1) or $success = 0;

if($success eq 1) {
	while(<DUMPFILE>) {
		if($state eq 0) {
			# first wait for the malformed LS request (47.11.11.47)
			if(/0\.0\.0\.10 > (.*) OSPFv2-ls_req (.*) 47\.11\.11\.47/) {
				$state = 1;
			}
		}
		if($state eq 1) {
			# now wait for an initial DD packet from 0.0.2.1 indicating state EXSTART
			if(/0\.0\.0\.9 > (.*) OSPFv2-dd (.*) I\/M\/MS/) {
				$state = 2;
			}
		}
	}
	
	close(DUMPFILE);
	
	if($state eq 2) {
		$local_verdict[0] = 1;
	} else {
		$local_verdict[0] = 2;
	}
}


$success = 1; 
$state = 0;

open(DUMPFILE, $dumpfile2) or $success = 0;

if($success eq 1) {
	while(<DUMPFILE>) {
		if($state eq 0) {
			# wait for the releant packets
			if(/0\.0\.0\.2 > (.*) OSPFv2-dd (.*) S 80000205/) {
				$state = 1;
			}
		}
		if($state eq 1) {
			# now wait for an initial DD packet from 0.0.0.1 indicating state EXSTART
			if(/0\.0\.0\.1 > (.*) OSPFv2-dd (.*) I\/M\/MS/) {
				$state = 2;
			}
		}
	}
	
	close(DUMPFILE);
	
	if($state eq 2) {
		$local_verdict[1] = 1;
	} else {
		$local_verdict[1] = 2;
	}
}

if($local_verdict[0] eq 1) {
	print("RUT transitions to EXSTART when requesting unknown LSA. Test PASSED.\n");
}elsif($local_verdict[0] eq 2) {
	print("RUT does not trasition to EXSTART when requesing unknown LSA. Test FAILED.\n");
} else {
	print("Cannot determine results. Test INCONCLUSIVE.\n");
}

if($local_verdict[1] eq 1) {
	print("RUT transitions to EXSTART when updating bad LSA. Test PASSED.\n");
}elsif($local_verdict[1] eq 2) {
	print("RUT does not trasition to EXSTART when updating bad LSA. Test FAILED.\n");
} else {
	print("Cannot determine results. Test INCONCLUSIVE.\n");
}

$verdict = $local_verdict[0];
if ($local_verdict[1] ne $verdict) {
	if($local_verdict[1] eq 2) {
		$verdict = 2;
	} elsif ($local_verdict[1] eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");